package bsts.view.customer;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import bsts.model.Customer;
import bsts.model.Interest;

import bsts.util.validation.TelephoneValidation;

public class UpdCustomerPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private JPanel pnlMain;
	private JPanel pnlBasicInfo;
	private JPanel pnlCustomer;
	private JPanel pnlInterest;
	private JPanel pnlInterestInfo;
	private JPanel pnlInterestBtns;
	private JPanel pnlInterestDisp;

	private GridBagConstraints gbcConst;

	private JLabel lblSurname;
	private JLabel lblFirstname;
	private JLabel lblSecondname;
	private JLabel lblNric;
	private JLabel lblAddress;
	private JLabel lblTelephoneNo;
	private JLabel lblSalutation;
	private JLabel lblInterest;
	private JLabel lblDescription;

	private JTextField txtSurname;
	private JTextField txtFirstname;
	private JTextField txtSecondname;
	private JTextField txtNric;
	private JScrollPane scrAddress = new JScrollPane();
	private JTextArea txtAddress;
	private JTextField txtTelephoneNo;
	private JComboBox cbxSalutation;
	private JTextField txtInterest;
	private JTextField txtDescription;

	private JTable tblInterests;
	private JScrollPane scrInterests;

	private JButton btnAddInterest;
	private JButton btnDelInterest;

	private ActionListener actlis_btnAddInterest;
	private ActionListener actlis_btnDelInterest;

	private Customer customer;
	
	private TelephoneValidation tv = new TelephoneValidation();

	public UpdCustomerPanel(Customer customer) {
		this.customer = customer;

		pnlMain = new JPanel();
		pnlBasicInfo = new JPanel();
		pnlCustomer = new JPanel();
		pnlInterest = new JPanel();
		pnlInterestInfo = new JPanel();
		pnlInterestBtns = new JPanel();
		pnlInterestDisp = new JPanel();

		pnlMain.setLayout(new BorderLayout());
		pnlCustomer.setLayout(new GridBagLayout());
		pnlInterest.setLayout(new BoxLayout(pnlInterest, BoxLayout.Y_AXIS));
		pnlInterestInfo.setLayout(new GridLayout(3, 2));
		pnlInterestBtns.setLayout(new FlowLayout(FlowLayout.RIGHT));
		pnlInterestDisp.setLayout(new BorderLayout());

		pnlBasicInfo.setBorder(BorderFactory.createTitledBorder("Basic Info"));
		pnlInterest.setBorder(BorderFactory.createTitledBorder("Interest"));

		gbcConst = new GridBagConstraints();

		lblSurname = new JLabel("Surname:");
		lblFirstname = new JLabel("First name:");
		lblSecondname = new JLabel("Second name:");
		lblNric = new JLabel("NRIC:");
		lblAddress = new JLabel("Address:");
		lblTelephoneNo = new JLabel("Telephone No.:");
		lblSalutation = new JLabel("Salutation:");
		lblInterest = new JLabel("Interest:");
		lblDescription = new JLabel("Remark:");

		txtSurname = new JTextField(customer.getSurname());
		txtFirstname = new JTextField(customer.getFirstname());
		txtSecondname = new JTextField(customer.getSecondname());
		txtNric = new JTextField(customer.getNric());
		txtNric.setEditable(false);
		txtAddress = new JTextArea(customer.getAddress(), 2, 20);
		txtTelephoneNo = new JTextField(customer.getTelephoneNo());
		cbxSalutation = new JComboBox();
		txtInterest = new JTextField();
		txtDescription = new JTextField();

		scrAddress
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrAddress.add(txtAddress, null);
		scrAddress.setViewportView(txtAddress);
		txtAddress.setEditable(true);
		txtAddress.setLineWrap(true);
		txtAddress.setAutoscrolls(true);
		txtSurname.setPreferredSize(new Dimension(150, 20));

		cbxSalutation.addItem(new String("Dr"));
		cbxSalutation.addItem(new String("Mr"));
		cbxSalutation.addItem(new String("Mrs"));
		cbxSalutation.addItem(new String("Miss"));
		cbxSalutation.addItem(new String("Mdm"));

		tblInterests = new JTable();
		scrInterests = new JScrollPane();

		initCbx();
		initTbl();
		scrInterests
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrInterests.add(tblInterests, null);
		scrInterests.setViewportView(tblInterests);
		scrInterests.setPreferredSize(new Dimension(500, 150));

		btnAddInterest = new JButton("Add");
		btnDelInterest = new JButton("Remove");

		gbcConst.fill = GridBagConstraints.HORIZONTAL;
		gbcConst.gridx = 0;
		gbcConst.gridy = 0;
		gbcConst.gridwidth = GridBagConstraints.RELATIVE;
		pnlCustomer.add(lblSurname, gbcConst);
		gbcConst.gridx = 1;
		pnlCustomer.add(txtSurname, gbcConst);
		gbcConst.gridx = 0;
		gbcConst.gridy = 1;
		pnlCustomer.add(lblFirstname, gbcConst);
		gbcConst.gridx = 1;
		pnlCustomer.add(txtFirstname, gbcConst);
		gbcConst.gridx = 0;
		gbcConst.gridy = 2;
		pnlCustomer.add(lblSecondname, gbcConst);
		gbcConst.gridx = 1;
		pnlCustomer.add(txtSecondname, gbcConst);
		gbcConst.gridx = 0;
		gbcConst.gridy = 3;
		pnlCustomer.add(lblNric, gbcConst);
		gbcConst.gridx = 1;
		pnlCustomer.add(txtNric, gbcConst);
		gbcConst.gridx = 0;
		gbcConst.gridy = 4;
		gbcConst.ipady = 20;
		pnlCustomer.add(lblAddress, gbcConst);
		gbcConst.gridx = 1;
		pnlCustomer.add(scrAddress, gbcConst);
		gbcConst.gridx = 0;
		gbcConst.gridy = 5;
		gbcConst.ipady = 0;
		pnlCustomer.add(lblTelephoneNo, gbcConst);
		gbcConst.gridx = 1;
		pnlCustomer.add(txtTelephoneNo, gbcConst);
		gbcConst.gridx = 0;
		gbcConst.gridy = 6;
		pnlCustomer.add(lblSalutation, gbcConst);
		gbcConst.gridx = 1;
		pnlCustomer.add(cbxSalutation, gbcConst);

		pnlBasicInfo.add(pnlCustomer);

		pnlInterestInfo.add(lblInterest);
		pnlInterestInfo.add(txtInterest);
		pnlInterestInfo.add(lblDescription);
		pnlInterestInfo.add(txtDescription);

		pnlInterestBtns.add(btnAddInterest);
		pnlInterestBtns.add(btnDelInterest);

		pnlInterestDisp.add(scrInterests, BorderLayout.CENTER);

		pnlInterest.add(pnlInterestInfo);
		pnlInterest.add(pnlInterestBtns);
		pnlInterest.add(pnlInterestDisp);

		pnlMain.add(pnlBasicInfo, BorderLayout.WEST);
		pnlMain.add(pnlInterest, BorderLayout.EAST);

		this.add(pnlMain);

		initActionListener();
		
		txtTelephoneNo.setInputVerifier(tv);
		txtTelephoneNo.addActionListener(tv);
	}

	public Customer updateCustomer() {
		customer.setSurname(txtSurname.getText());
		customer.setFirstname(txtFirstname.getText());
		customer.setSecondname(txtSecondname.getText());
		customer.setAddress(txtAddress.getText());
		customer.setTelephoneNo(txtTelephoneNo.getText());
		customer.setSalutation(cbxSalutation.getSelectedItem().toString());

		return customer;
	}

	public void refreshTbl() {
		tblInterests.revalidate();
		tblInterests.repaint();
	}

	private void initCbx() {
		for (int i = 0; i < cbxSalutation.getItemCount(); i++) {
			if (cbxSalutation.getItemAt(i).toString()
					.equals(customer.getSalutation())) {
				cbxSalutation.setSelectedIndex(i);
				break;
			}
		}
	}

	private void initTbl() {
		final String[] strHeaded = { "Interest", "Remark" };

		// Create a model of the data.
		TableModel dataModel = new AbstractTableModel() {
			private static final long serialVersionUID = 1L;

			public int getColumnCount() {
				return strHeaded.length;
			}

			public int getRowCount() {
				return customer.getInterests().size();
			}

			public Object getValueAt(int row, int col) {
				Interest interest = customer.getInterests().get(row);

				if (col == 0) {
					return interest.getName();
				} else if (col == 1) {
					return interest.getDescription();
				} else {
					return null;
				}
			}

			public String getColumnName(int column) {
				return strHeaded[column];
			}

			public Class<?> getColumnClass(int col) {
				return getValueAt(0, col).getClass();
			}
		};

		tblInterests = new JTable(dataModel);
		tblInterests.getSelectionModel();
		tblInterests.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		DefaultTableCellRenderer right = new DefaultTableCellRenderer();
		right.setHorizontalAlignment(DefaultTableCellRenderer.TRAILING);

		tblInterests.setDefaultRenderer(Object.class, right);

		int[] intWidth = { 200, 300 };

		for (int i = 0; i < intWidth.length; i++) {
			TableColumn tblCol = tblInterests.getColumn(strHeaded[i]);
			tblCol.setMaxWidth(intWidth[i]);
		}
	}

	private void initActionListener() {
		actlis_btnAddInterest = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (txtInterest.getText().trim().length() > 0) {
					Interest interest = new Interest(txtInterest.getText()
							.trim(), txtDescription.getText().trim());
					customer.getInterests().add(interest);

					refreshTbl();
				} else {
					JOptionPane.showMessageDialog(pnlMain,
							"Please enter the information for interest.");
				}
			}
		};

		actlis_btnDelInterest = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (tblInterests.getSelectedRow() >= 0) {
					customer.getInterests().remove(
							tblInterests.getSelectedRow());
					refreshTbl();
				} else {
					JOptionPane.showMessageDialog(pnlMain,
							"Please select a record.");
				}
			}
		};

		btnAddInterest.addActionListener(actlis_btnAddInterest);
		btnDelInterest.addActionListener(actlis_btnDelInterest);
	}

}
